package com.hopu.blb_erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.entity.TMenu;
import com.hopu.blb_erp.entity.TWarehouse;
import com.hopu.blb_erp.mapper.TMenuMapper;
import com.hopu.blb_erp.mapper.WarehouseMapper;
import com.hopu.blb_erp.service.ITMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.blb_erp.service.TMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TMenuServiceImpl extends ServiceImpl<TMenuMapper,TMenu> implements TMenuService {
    @Autowired
    private TMenuMapper tMenuMapper;

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public void modify(TMenu tMenu) {
        TMenu tMenu1 = tMenuMapper.selectById(tMenu.getId());
        if (tMenu1.isStatus()) {
            tMenu.setStatus(false);
            // 禁用仓库时将仓库下的所有货区禁用
            // 通过仓库id找对所有对应的货区
            List<TWarehouse> warehouses = warehouseMapper.selectList(new QueryWrapper<TWarehouse>().lambda().eq(TWarehouse::getWareId, tMenu.getId()));
            // 禁用货区
            warehouses.forEach(w -> {
                w.setStatus(false);
                warehouseMapper.updateById(w);
            });
        } else {
            tMenu.setStatus(true);
        }
        tMenuMapper.updateById(tMenu);
    }

    @Override
    public List<TMenu> findAllTMenus() {
        return tMenuMapper.selectAllTMenus();
    }

    @Override
    public List<TMenu> findAll() {
        return tMenuMapper.selectAll();
    }

    @Override
    public List<TMenu> findsAll() {
        return tMenuMapper.selectsAll();
    }

    @Override
    public PageEntity<TMenu> findPage(Integer current, Integer length) {
        Integer count = tMenuMapper.selectCounts();
       List<TMenu> tMenus = tMenuMapper.selectPages((current - 1) * length, length);
                tMenus.forEach(tMenu -> {
            System.out.println(tMenu.getTioWarehouses());
            System.out.println(tMenu.getTiomWarehouses());
        });
        PageEntity<TMenu> tMenuPageEntity = new PageEntity<>();
        tMenuPageEntity.setData(tMenus);
       tMenuPageEntity.setCurrent(current);
        tMenuPageEntity.setCount(count);
        return tMenuPageEntity;
    }
//
    @Override
    public List<TMenu> findById(Integer id) {
        return tMenuMapper.selectsById(id);
    }

    @Override
    public List<TMenu> selectWareNo(String wareNo) {
        return tMenuMapper.selectWareNo(wareNo);
    }


}